import http from '../api/httpRequest.js'
import { ENC_TYPE } from "../api/enums.js"
import {CryptoUtil} from "@/api/cryptoUtil";
export default {
	state: {
        ecriptionType: null,
        primaryKey: null,
        secondaryKey: null,
		instance: {
			cryptoUtil: new CryptoUtil(1),
			code: 1,
			keyObj: null,
			keyForKeyObj: null,
			sendCke: null,
		}
	},

	mutations: {
		setCryptoInstance(state, instance) {
			state.instance = instance
		},
		setEcriptionType(state, type) {
			state.ecriptionType = type
		},
        setPrimaryKey(state, key) {
			state.primaryKey = key
		},
        setSecondaryKey(state, key) {
			state.secondaryKey = key
		},
		clear(state) {
			state.ecriptionType = null
            state.primaryKey = null
            state.secondaryKey = null
		}
	},
	actions: {
		// encryptText(context, paramObj) {
		// 	return new Promise((resolve, reject) => {
		// 		var instance = context.state.instance
		// 		var sendCke = instance.sendCke
		// 		var encrypted = instance.cryptoUtil.encode(instance.keyObj, paramObj.message)
		// 		console.log('store encode', paramObj.message, encrypted)
		// 		resolve({
		// 			encrypted,
		// 			sendCke,
		// 			code: instance.code
		// 		})
		// 	});
		//
		// },
		// decryptText(context, paramObj) {
		// 	// return new Promise((resolve, reject) => {
		// 		var instance = context.state.instance
		// 		var keyForKeyObj = instance.keyForKeyObj
		//
		// 		var keyObj = instance.cryptoUtil.decode(keyForKeyObj, paramObj.keyEncry)
		//
		// 		var content = instance.cryptoUtil.decode(JSON.parse(keyObj), paramObj.content)
		//
		// 		console.log('store decryptText', content, paramObj)
		// 		// resolve(content)
		// 	return content
		// 	// });
		//
		// },// var sendCke = cryptoUtil.encode(keyForKeyObj, JSON.stringify(keyObj));
		// encryptKey(context, paramObj) {
		// 	return new Promise((resolve, reject) => {
		// 		var instance = context.state.instance
		// 		var keyForKeyObj = instance.keyForKeyObj
		// 		var keyObj = paramObj.keyObj
		//
		// 		// var sendCke = instance.sendCke
		// 		var keyObjEncrypted = instance.cryptoUtil.encode(keyForKeyObj, JSON.stringify(keyObj));
		//
		// 		// var encrypted = instance.cryptoUtil.encode(instance.keyObj, paramObj.message)
		// 		// console.log('store encode', encrypted)
		// 		resolve({
		// 			keyObjEncrypted
		// 		})
		// 	});
		//
		// },
		// decryptKey(context, paramObj) {
		// 	return new Promise((resolve, reject) => {
		// 		var instance = context.state.instance
		// 		var keyForKeyObj = instance.keyForKeyObj
		//
		// 		// var sendCke = instance.sendCke
		// 		var keyObj = instance.cryptoUtil.decode(keyForKeyObj, paramObj.keyEncry)
		// 		resolve({
		// 			keyObj
		// 		})
		// 	});
		//
		// },
		// encryptFile(context, paramObj) {
		// 	return new Promise((resolve, reject) => {
		// 		var code = 0
		// 		var keyObj = {
		// 			code,
		// 			key: '12345678'
		// 		}
		// 		var reader = new FileReader();
		// 		reader.readAsArrayBuffer(paramObj.file);
		// 		var filemeta = {
		// 			name: paramObj.file.name,
		// 			type: paramObj.file.type,
		// 		}
		// 		reader.onload = () => {
		// 			console.log('reader', reader.result);
		// 			var fileEncrypted = new CryptoUtil(code).encryptFileImpl(reader.result, keyObj.key, filemeta)
		// 			resolve(fileEncrypted)
		// 		}
		// 	});
		// },
		// decryptFile(context, paramObj) {
		// 	var url = paramObj.url
		// 	return new Promise((resolve, reject) => {
		// 		fetch(url).then(res => res.blob()).then(file => {
		// 			var code = 0
		// 			var keyObj = {
		// 				code,
		// 				key: '12345678'
		// 			}
		// 			console.log('fetch', file);
		//
		// 			var reader = new FileReader();
		// 			reader.readAsText(file);
		// 			reader.onload = () => {
		// 				console.log('reader.result', reader.result);
		// 				var decryptedFile = new CryptoUtil(code).decryptFile(reader.result, keyObj.key);
		// 				console.log('decryptedFile', decryptedFile)
		// 				resolve(decryptedFile)
		// 			}
		// 		}).catch(e=>{
		// 			console.log(e);
		// 			resolve(null)
		// 		})
		// 	});
		// },
		// loadEncryption(context, code) {
		loadEncryption(context) {
			return new Promise((resolve, reject) => {
				var code = Number(sessionStorage.getItem("code"));
				var cryptoUtil = new CryptoUtil(code);
				var keyObj = cryptoUtil.randomKeyObj(null);
				var keyForKeyObj = cryptoUtil.randomKeyObj(sessionStorage.getItem("psha256"))
				var sendCke = cryptoUtil.encode(keyForKeyObj, JSON.stringify(keyObj));

				var instance = {
					code,
					cryptoUtil,
					keyObj,
					keyForKeyObj,
					sendCke,
				}

				context.commit("setCryptoInstance", instance);
				resolve();
			})

			/*return new Promise((resolve, reject) => {
				http({
					url: '/encryptionMode/get',
					method: 'POST',
				}).then((encInfo) => {
                    console.log("******:", encInfo.data)
					// context.commit("setEcriptionType", encInfo.data)
					resolve();
				}).catch((res) => {
					reject(res);
				});
			})*/
		}
	},
	getters: {
		cryptoInstance: (state) => () => {
			return state.instance
		},
	}
}